package com.brownalexis.application.utils;

import com.brownalexis.application.enums.ResponseCode;
import lombok.Data;

import java.io.Serializable;

/**
 *@Author: brownalexis
 * @Date: 2024/04/01 14:42
 * @Description:
 * 统一API响应结果封装。
 */
@Data
public class ApiResponse<T> implements Serializable {
    private int code;
    private T data;
    private String message;

    private ApiResponse() {
    }

    public static <T> ApiResponse<T> success() {
        return success(null);
    }

    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.code = ResponseCode.SUCCESS.getCode();
        response.message = ResponseCode.SUCCESS.getMessage();
        response.data = data;
        return response;
    }

    public static <T> ApiResponse<T> error(ResponseCode responseCode) {
        ApiResponse<T> response = new ApiResponse<>();
        response.code = responseCode.getCode();
        response.message = responseCode.getMessage();
        return response;
    }

    public static <T> ApiResponse<T> error(ResponseCode responseCode,String message) {
        ApiResponse<T> response = new ApiResponse<>();
        response.code = responseCode.getCode();
        response.message = message;
        return response;
    }


}

